<?
class validator {
	var $init_validate_array = array();
	var $validate_mask = array();
	var $validate_returned = array();
	var $backup_validator = array();
	
	function validator($validate_array){
		$this->init_validate_array = $validate_array;
		foreach($this->init_validate_array as $k=>$v){
			$this->validate_mask[$k] = $this->init_validate_array[$k]['check_this'];
		}
	}

	function validate_value($key,$value){
		if($this->init_validate_array[$key] and $this->init_validate_array[$key]['pattern'] and preg_match($this->init_validate_array[$key]['pattern'],$value)){
			return true;
		}else {
			return false;
		}
	}
	
	function validate_array($key_array,$strinct_mode=true,$array_filter=array(),$mask_array=array()){
		$mask_array = array_merge($this->validate_mask,$mask_array);
		foreach($this->init_validate_array as $k=>$v){
			if(!$strinct_mode and !isset($key_array[$k])){
				$mask_array[$k] = -1;
			}
			if($mask_array[$k]==-1){
				$this->validate_returned[$k] = 1;
			}elseif($mask_array[$k]==0 and !$key_array[$k]){
				$this->validate_returned[$k] = 1;
			}elseif($mask_array[$k]>=0 and $key_array[$k] and $v['pattern'] and preg_match($v['pattern'],$key_array[$k])){
				$this->validate_returned[$k] = 1;
			}else{
				$this->validate_returned[$k] = 0;
			}
		}
		if(in_array(0,array_values($this->validate_returned))){
			return false;
		}elseif (!empty($array_filter) and is_array($array_filter)){
			foreach($array_filter as $k=>$v){
				$ret[$v] = $key_array[$v];
			}
			return $ret;
		}else{
			return true;
		}
	}
	
	function change_validator($new_validator,$action='merge'){
		$this->backup_validator = $this->init_validate_array;
		if($action=='merge'){
			$this->backup_validator = $this->init_validate_array;
			$this->init_validate_array = array_merge($this->init_validate_array,$new_validator);
		}elseif($action=='replace'){
		
		}
	}

	function restore_validator(){
		if(!empty($this->backup_validator)){
			$this->init_validate_array = $this->backup_validator;
			$this->backup_validator = array();
		}
	}
}
?>